<?php
class ServiceModel extends Model {
    public function __construct() {
        parent::__construct();
        $this->check = new ServiceCheck();
        $this->tables = array(DB_FREFIX.'service');
        $this->fields = array('id','name','content','selected','date');
        list(
            $this->R['id'],
            $this->R['name']
        ) = $this->getRequest()->getParams(
            array(
               isset($_GET['id']) ? $_GET['id'] : null,
               isset($_POST['name']) ? $_POST['name'] : null
            )
        );
    }

    public function findOne() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->oneCheck($this,$condition)) $this->check->displayError();
        return parent::select($this->tables,array('id','name','content','selected'),array('where'=>$condition,'limit'=>1));
    }


    public function findAll() {
        return parent::select($this->tables,array('id','name','selected'),array('limit'=>$this->limit,'order'=>'selected DESC,date DESC'));
    }

    public function add() {
        $condition = array('name='.$this->R['name']);
        if(!$this->check->addCheck($this,$condition)) $this->check->displayError();
        $needData = $this->getRequest()->filter($this->fields);
        $needData['date'] = Tool::getDate();
        if($needData['selected'] == 1) {
            $condition = array('selected=1');
            $updateData['selected'] = 0;
            parent::update($updateData,$condition);
        }
        return parent::add($needData);
    }

    public function update() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->oneCheck($this, $condition)) $this->check->displayError();
        $updateData = $this->getRequest()->filter($this->fields);
        if($updateData['selected'] == 1) {
            $condition2 = array('selected=1');
            $oldData['selected'] = 0;
            parent::update($oldData,$condition2);
        }
        return parent::update($updateData,$condition);
    }

    public function delete() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->oneCheck($this, $condition)) $this->check->displayError();
        return parent::delete($condition);
    }

    public function selected() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->oneCheck($this, $condition)) $this->check->displayError();
        $condition = array('selected=1');
        $updateData['selected'] = 0;
        parent::update($updateData,$condition);
        $condition = array('id='.$this->R['id']);
        $updateData['selected'] = 1;
        return parent::update($updateData,$condition);
    }

    public function findAddGoodsService() {
        $allService = parent::select(array(DB_FREFIX.'service'),array('id','name'));
        $allService = Tool::setObjectArray($allService,'id','name');
        return $allService;
    }


    public function findAddGoodsServiceSelected() {
        $condition = array('selected=1');
        $service = parent::select(array(DB_FREFIX.'service'),array('id'),array('where'=>$condition));
        return $service[0]->id;
    }

    public function ajaxCheckName() {
        $condition = array('name='.$this->R['name']);
        $this->check->ajaxCheckName($this,$condition);
    }

    public function total() {
        return parent::total();
    }

}




?>